const path = require("path");
// 模块热更新
const webpack = require("webpack");
const HtmlWebpackPlugin = require("html-webpack-plugin");

module.exports = {
  mode: "development",
  entry: "./src/index.tsx",
  output: {
    path: path.resolve(__dirname, "dist"),
    filename: "bundle.js",
  },
  module: {
    rules: [
      {
        test: /\.(ts|tsx)$/, // 匹配 .ts 和 .tsx 文件
        exclude: /node_modules/,
        use: {
          loader: "ts-loader",
        },
      },
      {
        // 匹配.js和.jsx文件
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        use: {
          loader: "babel-loader",
          options: {
            presets: ["@babel/preset-env", "@babel/preset-react"],
          },
        },
      },
      {
        test: /\.(less|css)$/, // 匹配.less文件
        use: [
          {
            loader: "style-loader", // 创建<style>标签并将CSS插入到其中
          },
          {
            loader: "css-loader", // 将CSS转换为CommonJS模块
          },
          {
            loader: "less-loader", // 将LESS转换为CSS
            options: {
              // 可选的LESS配置选项
            },
          },
        ],
      },
    ],
  },
  // 解析选项，帮助Webpack解析模块路径
  resolve: {
    extensions: [".tsx", ".ts", ".js", ".jsx"], // 自动解析确定的扩展名
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: "./public/index.html",
      filename: "index.html",
    }),
  ],
  devServer: {
    static: {
      directory: path.join(__dirname, "dist"),
    },
    hot: true,
    compress: true,
    port: 3000,
  },
};
